//
//  LXMySaleViewController.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/8/17.
//  Copyright © 2016年 SJT. All rights reserved.
//............................................................................................//
//...........................................个人中心-我的特卖...........................................//
//............................................................................................//

#import "LXMySaleViewController.h"
#import "LXBrandViewController.h"
#import "LXGoodsViewController.h"
#import "LXWatchHistoryViewController.h"
#import "LXMySaleButton.h"

@interface LXMySaleViewController ()<UIScrollViewDelegate>

/**存放所以子控制器的scrollView*/
@property(nonatomic,weak) UIScrollView *scrollView;
/**标题栏*/
@property(nonatomic,weak) UIView *titlesView;
/**当前被选中的按钮*/
@property(nonatomic,weak) LXMySaleButton *selectedTitleButton;
/**标题栏指示器*/
@property(nonatomic,weak) UIView *titleBottomView;
/**存放所有的标题按钮*/
@property(nonatomic,strong) NSMutableArray *titleButtons;

@end

@implementation LXMySaleViewController

- (NSMutableArray *)titleButtons
{
    if (!_titleButtons) {
        _titleButtons = [NSMutableArray array];
    }
    
    return _titleButtons;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addAllChildVc];
    [self setUpTitlesView];
    [self setUpScrollView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpNavi];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)setUpNavi{
    [super setUpNavi];
//    UINavigationBar *  navigationBar = self.navigationController.navigationBar;
    
    self.navigationItem.title = @"我的特卖";
}

- (void)addAllChildVc
{
    LXBrandViewController *brandVc = [[LXBrandViewController alloc] init];
    brandVc.title = @"品牌";
    [self addChildViewController:brandVc];
    
    LXGoodsViewController *goodsVc = [[LXGoodsViewController alloc] init];
    goodsVc.title = @"商品";
    [self addChildViewController:goodsVc];
    
    LXWatchHistoryViewController *watchHistoryVc = [[LXWatchHistoryViewController alloc] init];
    watchHistoryVc.title = @"浏览记录";
    [self addChildViewController:watchHistoryVc];
}

- (void)setUpTitlesView
{
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor whiteColor];
    titlesView.frame = CGRectMake(0, 0, self.view.width, 44);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    NSInteger count = self.childViewControllers.count;
    CGFloat titleButtonH = titlesView.height;
    CGFloat titleButtonW = titlesView.width / count;
    CGFloat titleButtonX = 0;
    CGFloat titleButtonY = 0;
    for (int i = 0; i < count; i++) {
        LXMySaleButton *titleButton = [LXMySaleButton buttonWithType:UIButtonTypeCustom];
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        titleButtonX = i * titleButtonW;
        titleButton.frame = CGRectMake(titleButtonX, titleButtonY, titleButtonW, titleButtonH);
        [titlesView addSubview:titleButton];
        [self.titleButtons addObject:titleButton];
        
        NSString *title = [self.childViewControllers[i] title];
        [titleButton setTitle:title forState:UIControlStateNormal];
        
        titleButton.y = 0;
        titleButton.height = titleButtonH;
        titleButton.width = titleButtonW;
        titleButton.x = i * titleButton.width;
    }
    
    UIView *titleBottomView = [[UIView alloc] init];
    titleBottomView.backgroundColor = [self.titleButtons.lastObject titleColorForState:UIControlStateSelected];
    titleBottomView.height = 1;
    titleBottomView.y = titlesView.height - titleBottomView.height;
    [titlesView addSubview:titleBottomView];
    self.titleBottomView = titleBottomView;
    
    LXMySaleButton *firstTitleButton = self.titleButtons.firstObject;
    titleBottomView.width = firstTitleButton.width;
    titleBottomView.centerX = firstTitleButton.centerX;
    
    [self titleButtonClick:firstTitleButton];
}

- (void)setUpScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.y = self.titlesView.height;
    scrollView.backgroundColor = [UIColor lightGrayColor];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(self.childViewControllers.count * self.view.width, 0);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}


- (void)titleButtonClick:(LXMySaleButton *)titleButton
{
    self.selectedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.selectedTitleButton = titleButton;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.titleBottomView.width = titleButton.width;
        self.titleBottomView.centerX = titleButton.centerX;
    }];
    
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = self.view.width * [self.titleButtons indexOfObject:titleButton];
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    UIViewController *willShowVc = self.childViewControllers[index];
    
    if (willShowVc.isViewLoaded) return;
    
    willShowVc.view.frame = scrollView.bounds;
    [scrollView addSubview:willShowVc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    int index = scrollView.contentOffset.x / scrollView.bounds.size.width;

    [self titleButtonClick:self.titleButtons[index]];
    
}

@end
